/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.i.kotionokyurievna.jewelryweb.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.*;

/**
 *
 * @author Kateryna_Reznik
 */
@Entity
@NamedQueries({
    @NamedQuery ( name = "Person.findByFirstName ", 
        query="SELECT p.firstName, p.lastName, p.birthDate, "
        + "p.telephone, p.email, p.gender "
        + "FROM Person p "
        + "WHERE p.firstName = :firstName"),
    @NamedQuery ( name = "Person.findBySecondName ", 
        query="SELECT p.firstName, p.lastName, p.birthDate, "
        + "p.telephone, p.email, p.gender "
        + "FROM Person p "
        + "WHERE p.lastName = :lastName"),
    @NamedQuery ( name = "Person.findByName ", 
        query="SELECT p.firstName, p.lastName, p.birthDate, "
        + "p.telephone, p.email, p.gender "
        + "FROM Person p "
        + "WHERE p.firstName = :firstName AND p.lastName = :secondName"),
    @NamedQuery ( name = "Person.findByBirthDate ", 
        query="SELECT p.firstName, p.lastName, p.birthDate, "
        + "p.telephone, p.email, p.gender "
        + "FROM Person p "
        + "WHERE p.birthDate = :birthDate"),
    @NamedQuery ( name = "Person.findByTelephone ", 
        query="SELECT p.firstName, p.lastName, p.birthDate, "
        + "p.telephone, p.email, p.gender "
        + "FROM Person p "
        + "WHERE p.telephone = :telephone"),
    @NamedQuery ( name = "Person.findByEmail ", 
        query="SELECT p.firstName, p.lastName, p.birthDate, "
        + "p.telephone, p.email, p.gender "
        + "FROM Person p "
        + "WHERE p.email = :email"),
    @NamedQuery ( name = "Person.findByGender ", 
        query="SELECT p.firstName, p.lastName, p.birthDate, "
        + "p.telephone, p.email, p.gender "
        + "FROM Person p "
        + "WHERE p.gender = :gender")
})
@Table ( name = "persons")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person extends User implements Serializable{
    @Column (name = "first_name")
    private String firstName;
    @Column (name = "second_name")
    private String lastName;
    @Column (name = "birth_date")
    @Temporal (value = TemporalType.DATE)
    private Date birthDate;
    private String telephone;
    private String email;
    @Enumerated (EnumType.STRING)
    private Gender gender;
    
    
    public Person(){
        
    }

    public Person(String firstName, String lastName, Date birthDate, 
            String telephone, String email, Gender gender, String login,
            String password) {
        super(login, password);
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
        this.telephone = telephone;
        this.email = email;
        this.gender = gender;
    }
    
    public Person(String firstName, String lastName, Date birthDate, 
            String telephone, String email, Gender gender, String login,
            String password, UserRole userRole) {
        super(login, password, userRole);
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
        this.telephone = telephone;
        this.email = email;
        this.gender = gender;
    }
    
    public Person(String firstName, String lastName, Date birthDate, 
            String telephone, String email, Gender gender, User user) {
        super(user);
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
        this.telephone = telephone;
        this.email = email;
        this.gender = gender;
    }
    
    public Person(int personId, String firstName, String lastName, 
            Date birthDate, String telephone, String email, 
            Gender gender, String login, String password) {
        super(login, password);
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
        this.telephone = telephone;
        this.email = email;
        this.gender = gender;
    }
    
     public Person(int personId, String firstName, String lastName, 
            Date birthDate, String telephone, String email, 
            Gender gender, String login, String password, UserRole userRole) {
        super(login, password, userRole);
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
        this.telephone = telephone;
        this.email = email;
        this.gender = gender;
    }

    public Person(int personId, String firstName, String lastName, 
            Date birthDate, String telephone, String email, 
            Gender gender, User user) {
        super(user);
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
        this.telephone = telephone;
        this.email = email;
        this.gender = gender;
    }
    
    public Person ( Person another, User user ){
        super (user);
        if ( another != null ){
            this.firstName = another.firstName;
            this.lastName = another.lastName;
            this.birthDate = another.birthDate;
            this.telephone = another.telephone;
            this.email = another.email;
            this.gender = another.gender;
        }
    }

    public String getFirstName() {
        return firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }
    
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getBirthDate() {
        return birthDate;
    }
    
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public String getTelephone() {
        return telephone;
    }
    
    public boolean setTelephone(String telephone) {
        Pattern p = Pattern.compile("(\\+){1}([0-9]){12}");
        Matcher m = p.matcher(telephone);
        if ( m.matches() ){
            this.telephone = telephone;
            return true;
        }
        else{
            if ( telephone == null ){
                this.telephone = "";
            }
        }
        return false;
    }

    public String getEmail() {
        return email;
    }
    
    public boolean setEmail(String email) {
        
        Pattern p = Pattern.compile(".*(@){1}.*");
        Matcher m = p.matcher(email);
        if ( m.matches() ){
            this.email = email;
            return true;
        }
        else{
            if ( email == null ){
                this.email = email;
            }
        }
        return false;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person{" + "firstName=" + firstName + ", lastName=" + lastName + 
                ", birthDate=" + birthDate + ", telephone=" + telephone + 
                ", email=" + email + ", gender=" + gender + '}';
    }    

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + (this.firstName != null ? this.firstName.hashCode() : 0);
        hash = 29 * hash + (this.lastName != null ? this.lastName.hashCode() : 0);
        hash = 29 * hash + (this.birthDate != null ? this.birthDate.hashCode() : 0);
        hash = 29 * hash + (this.telephone != null ? this.telephone.hashCode() : 0);
        hash = 29 * hash + (this.email != null ? this.email.hashCode() : 0);
        hash = 29 * hash + (this.gender != null ? this.gender.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if ((this.firstName == null) ? (other.firstName != null) : 
                !this.firstName.equals(other.firstName)) {
            return false;
        }
        if ((this.lastName == null) ? (other.lastName != null) : 
                !this.lastName.equals(other.lastName)) {
            return false;
        }
        if (this.birthDate != other.birthDate && (this.birthDate == null || 
                !this.birthDate.equals(other.birthDate))) {
            return false;
        }
        if ((this.telephone == null) ? (other.telephone != null) : 
                !this.telephone.equals(other.telephone)) {
            return false;
        }
        if ((this.email == null) ? (other.email != null) : 
                !this.email.equals(other.email)) {
            return false;
        }
        if (this.gender != other.gender) {
            return false;
        }
        return true;
    }
    
    
}
